package net.muchine.test.ddd.repository.hibernate;

import java.util.List;

import net.muchine.test.ddd.domain.Customer;
import net.muchine.test.ddd.repository.CustomerRepository;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HibernateCustomerRepository implements
		CustomerRepository {
	
	@Autowired private SessionFactory factory;

	public void save(Customer customer) {
		this.factory.getCurrentSession().save(customer);
	}

	public Customer delete(String customerId) {
		Customer customer = this.find(customerId);
		
		this.factory.getCurrentSession().delete(customer);
		return null;
	}

	@SuppressWarnings("unchecked")
	public Customer find(String customerId) {
		Query query = this.factory.getCurrentSession().createQuery("from Customer where customerNumber=:number")
				.setString("number", customerId);
		Customer customer = (Customer) query.uniqueResult();
		
		return customer;
	}

}
